package com.gf.pf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gf.common.BaseController;
import com.gf.common.Md5Encryption;
import com.gf.pf.model.Department;
import com.gf.pf.model.Job;
import com.gf.pf.model.Resource;
import com.gf.pf.model.Role;
import com.gf.pf.model.User;
import com.gf.pf.service.DepartmentService;
import com.gf.pf.service.JobService;
import com.gf.pf.service.ResourceService;
import com.gf.pf.service.RoleService;
import com.gf.pf.service.UserService;

/**
 * 
 * @describe 登录
 * @date     2014年9月3日
 */
@Controller
public class LoginController extends BaseController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
	
	/**
	 * 用户登录地址
	 */
	private String USER_LOGIN = "login";
	
	/**
	 * 登录成功后的地址
	 */
	private String SYSTEM_INDEX = "index";
	
	/**
	 * 登录失败
	 */
	private String LOGIN_ERROR = "loginError";
	
	/**
	 * 用户名或密码不能为
	 */
	private String userName_password_notEmpty = "用户名或密码不能为空";
	
	/**
	 * 用户名或密码错误
	 */
	private String userName_password_error = "用户名或密码错误";

	@Autowired
	private UserService userService;
	
	@Autowired
	private DepartmentService departmentServcie;
	
	@Autowired
	private JobService jobService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 
	 * @describe 用户登录
	 * @return
	 */
	@RequestMapping(value = "jlogin", method = RequestMethod.GET)
	public String loginForward() {
		return this.USER_LOGIN;
	}

	/**
	 * 
	 * @describe 用户登录验证
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(String username, String password, HttpServletRequest request, Model model) {
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			LOGGER.error(this.userName_password_notEmpty);
			model.addAttribute(this.LOGIN_ERROR, this.userName_password_notEmpty);
			return this.USER_LOGIN;
		}
		
		//组装参数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", username);
		map.put("password", Md5Encryption.getInstance().md5Encode(password));
		
		User user = userService.getUser(map);
		if(null == user) {
			LOGGER.error(this.userName_password_error);
			model.addAttribute(this.LOGIN_ERROR, this.userName_password_error);
			return this.USER_LOGIN;
		}
		
		//部门
		Department dep = departmentServcie.getDep(user.getUserDepartmentId());
		user.setDepartment(dep);
		
		//职位
		List<Job> jobs = jobService.getJobs(user.getId());
		user.setJobList(jobs);
		
		//角色
		List<Role> roles = roleService.getRoles(user.getId());
		user.setRoleList(roles);
		
		//一级菜单
		List<Resource> yiJiResources = resourceService.getYiJiResources(user.getId());
		if(null != yiJiResources) {
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("userId", user.getId());
			for(Resource yiJiResource : yiJiResources) {
				maps.put("parentId", yiJiResource.getId());
				yiJiResource.setErJiResourceList(resourceService.getErJiResources(maps));
			}
			user.setResourceList(yiJiResources);
		}
		
		HttpSession session = request.getSession();
		session.setAttribute(super.USER_SESSION_KEY, user);
		
		return this.SYSTEM_INDEX;
	}
	
	/**
	 * 
	 * @describe 退出
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.invalidate();
		return this.USER_LOGIN;
	}
	
}
